home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / HexEdit 1.21 / ~Project / Source / Menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-03  |  7.7 KB  |  310 lines  |  [TEXT/CWIE]

  1. /*********************************************************************
  2.  * Menus.c
  3.  *
  4.  * HexEdit, a simple hex editor
  5.  * Copyright 1993, Jim Bumgardner
  6.  *
  7.  * Modifications by Lane Roathe, Ideas From the Deep, marked "//LR"
  8.  *********************************************************************/
  9. #include "HexEdit.h"
  10.  
  11. // Menu Handles
  12. MenuHandle    gAppleMenu, gFileMenu, gEditMenu, gFindMenu, gOptionsMenu;
  13. extern short CompareFlag;
  14. extern WindowPtr CompWind1,CompWind2;
  15. extern short gForkMode;
  16.  
  17. // Menu Initialization code.
  18.  
  19. void MySetUpMenus(void)
  20. {
  21.     Handle    myMenuBar;
  22.     myMenuBar = GetNewMBar(MenuBaseID);
  23.     SetMenuBar(myMenuBar);
  24.  
  25.     gAppleMenu = GetMenuHandle(AppleMENU);
  26.     gFileMenu = GetMenuHandle(FileMENU);
  27.     gEditMenu = GetMenuHandle(EditMENU);
  28.     gFindMenu = GetMenuHandle(FindMENU);
  29.     gOptionsMenu = GetMenuHandle(OptionsMENU);
  30.  
  31.     AppendResMenu(gAppleMenu, 'DRVR');
  32.  
  33.     DrawMenuBar();
  34. }
  35.  
  36.  
  37. //    Enable or disable the items in the Edit menu if a DA window
  38. //    comes up or goes away. Our application doesn't do anything with
  39. //    the Edit menu.
  40.  
  41. void MyEnableMenuItem (MenuHandle menu, short item, short ok);
  42.  
  43. void MyAdjustMenus(void)
  44. {
  45.     register WindowPeek wp;
  46.     short                 windowKind;
  47.     Boolean             isDA,isObjectWindow,selection,scrapExists,undoExists;
  48.     EditWindowPtr        dWin;
  49.  
  50.     wp = (WindowPeek) FrontWindow();
  51.     dWin = (EditWindowPtr) wp;
  52.     windowKind = wp ? wp->windowKind : 0;
  53.     isDA = (windowKind < 0);
  54.     isObjectWindow = (wp? wp->refCon == MyWindowID : false);
  55.  
  56.     selection = (isObjectWindow && 
  57.             ((EditWindowPtr) wp)->endSel > ((EditWindowPtr) wp)->startSel);
  58.  
  59.     scrapExists = (isObjectWindow && gScrapChunk != NULL);
  60.  
  61.     undoExists = (gUndoRec.type != 0);
  62.  
  63.     MyEnableMenuItem(gEditMenu, 0, wp != NULL);
  64.     MyEnableMenuItem(gEditMenu, EM_Undo, isDA || undoExists);
  65.     MyEnableMenuItem(gEditMenu, EM_Cut, isDA || selection);
  66.     MyEnableMenuItem(gEditMenu, EM_Copy, isDA || selection);
  67.     MyEnableMenuItem(gEditMenu, EM_Paste, isDA || scrapExists);
  68.     MyEnableMenuItem(gEditMenu, EM_Clear, isDA || selection);
  69.  
  70.     MyEnableMenuItem(gEditMenu, EM_SelectAll, isDA || isObjectWindow);
  71.  
  72.     MyEnableMenuItem(gFileMenu, FM_New, gSearchWin == NULL);
  73.     MyEnableMenuItem(gFileMenu, FM_Open, gSearchWin == NULL);
  74.     if (isObjectWindow && dWin->startSel < dWin->endSel)
  75.         SetMenuItemText(gFileMenu, FM_Print, "\pPrint Selection…");
  76.     else
  77.         SetMenuItemText(gFileMenu, FM_Print, "\pPrint…");
  78.  
  79.     MyEnableMenuItem(gFileMenu, FM_Print, isObjectWindow);
  80.     MyEnableMenuItem(gFileMenu, FM_Close, isDA || isObjectWindow);
  81.     MyEnableMenuItem(gFileMenu, FM_Save, isObjectWindow && dWin->dirtyFlag);
  82.     MyEnableMenuItem(gFileMenu, FM_SaveAs, isObjectWindow);
  83.     MyEnableMenuItem(gFileMenu, FM_Revert, isObjectWindow && dWin->refNum &&
  84.                                         dWin->dirtyFlag);
  85.  
  86.     MyEnableMenuItem(gFindMenu, 0, isObjectWindow);
  87.     MyEnableMenuItem(gFindMenu, SM_Find, isObjectWindow);
  88.     MyEnableMenuItem(gFindMenu, SM_FindForward, isObjectWindow);
  89.     MyEnableMenuItem(gFindMenu, SM_FindBackward, isObjectWindow);
  90.     MyEnableMenuItem(gFindMenu, SM_GotoAddress, isObjectWindow);
  91.  
  92.     CheckItem(gOptionsMenu, OM_HiAscii, gPrefs.asciiMode == AM_Hi);
  93.     CheckItem(gOptionsMenu, OM_DecimalAddr, gPrefs.decimalAddr);
  94.     CheckItem(gOptionsMenu, OM_Backups, gPrefs.backupFlag);
  95.     CheckItem(gOptionsMenu, OM_Overwrite, gOverwrite);
  96. }
  97.  
  98.  
  99. // Code to simplify enabling/disabling menu items.
  100. //
  101. static void MyEnableMenuItem(MenuHandle menu, short item, short ok)
  102. {
  103.     if (ok)
  104.         EnableItem(menu, item);
  105.     else
  106.         DisableItem(menu, item);
  107.     if (item == 0)
  108.         DrawMenuBar();
  109. }
  110.  
  111.  
  112. //    Handle the menu selection. mSelect is what MenuSelect() and
  113. //    MenuKey() return: the high word is the menu ID, the low word
  114. //    is the menu item
  115. //
  116. void MyHandleMenu (long mSelect)
  117.  
  118. {
  119.     int            menuID = HiWord(mSelect);
  120.     int            menuItem = LoWord(mSelect);
  121.     Str255        name;
  122.     GrafPtr        savePort;
  123.     WindowPeek    frontWindow;
  124.     EditWindowPtr    dWin;
  125.  
  126.     switch (menuID) {
  127.     case AppleMENU:
  128.         if (menuItem == AM_About) {
  129.             HexEditAboutBox();
  130.         }
  131.         else {
  132.             GetPort(&savePort);
  133.             GetMenuItemText(gAppleMenu, menuItem, name);
  134.             OpenDeskAcc(name);
  135.             SetPort(savePort);
  136.         }
  137.         break;
  138.     case FileMENU:
  139.         switch (menuItem) {
  140.         case FM_New:
  141.             NewEditWindow();
  142.             break;
  143.         case FM_Open:
  144.             AskEditWindow();
  145.             break;
  146.         case FM_Save:
  147.             if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  148.                 break;
  149.             if (frontWindow->refCon == MyWindowID &&
  150.                 ((ObjectWindowPtr) frontWindow)->Save) {
  151.                 ((ObjectWindowPtr) frontWindow)->Save((WindowPtr) frontWindow);
  152.             }
  153.             break;
  154.         case FM_SaveAs:
  155.             if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  156.                 break;
  157.             if (frontWindow->refCon == MyWindowID &&
  158.                 ((ObjectWindowPtr) frontWindow)->SaveAs) {
  159.                 ((ObjectWindowPtr) frontWindow)->SaveAs((WindowPtr) frontWindow);
  160.             }
  161.             break;
  162.         case FM_Revert:
  163.             if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  164.                 break;
  165.             if (frontWindow->refCon == MyWindowID &&
  166.                 ((ObjectWindowPtr) frontWindow)->Revert) {
  167.                 ((ObjectWindowPtr) frontWindow)->Revert((WindowPtr) frontWindow);
  168.             }
  169.             break;
  170.  
  171.         case FM_Close:
  172.             if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  173.                 break;
  174.              if (frontWindow->windowKind < 0)
  175.                  CloseDeskAcc(frontWindow->windowKind);
  176.             else if (frontWindow->refCon == MyWindowID) {
  177.                 CloseEditWindow((WindowPtr) frontWindow);
  178.             }
  179.             else if ((WindowPtr) frontWindow == gSearchWin) {
  180.                 DisposeDialog(gSearchWin);
  181.                 gSearchWin = NULL;
  182.             }
  183.  
  184.               break;
  185.  
  186.         case FM_Quit:
  187.             if (CloseAllEditWindows())
  188.                 gQuitFlag = true;
  189.             break;
  190.         case FM_PageSetup:
  191.             PrOpen();
  192.             PrStlDialog(gHPrint);
  193.             PrClose();
  194.             break;
  195.         case FM_Print:
  196.             dWin = (EditWindowPtr) FrontWindow();
  197.             PrintWindow(dWin);
  198.             break;
  199.         }
  200.         break;
  201.     case EditMENU:
  202.         if (!SystemEdit(menuItem-1)) {
  203.             dWin = (EditWindowPtr) FrontWindow();
  204.             switch (menuItem) {
  205.             case EM_Undo:
  206.                 UndoOperation();
  207.                 break;
  208.             case EM_Cut:
  209.                 CutSelection(dWin);                
  210.                 break;
  211.             case EM_Copy:    
  212.                 CopySelection(dWin);    
  213.                 break;
  214.             case EM_Paste:    
  215.                 PasteSelection(dWin);    
  216.                 break;
  217.             case EM_Clear:
  218.                 ClearSelection(dWin);            
  219.                 break;
  220.             case EM_SelectAll:
  221.                 dWin = (EditWindowPtr) FrontWindow();
  222.                 dWin->startSel = 0;
  223.                 dWin->endSel = dWin->fileSize;
  224.                 UpdateOnscreen((WindowPtr) dWin);
  225.                 break;
  226.             }
  227.         }
  228.         break;
  229.     case FindMENU:
  230.         dWin = (EditWindowPtr) FrontWindow();
  231.         if (((WindowPeek) dWin)->refCon == MyWindowID) {
  232.             switch (menuItem) {
  233.             case SM_Find:
  234.                 OpenSearchDialog();
  235.                 break;
  236.             case SM_FindForward:
  237.                 gSearchDir = 0;
  238.                 PerformTextSearch(dWin);
  239.                 break;
  240.             case SM_FindBackward:
  241.                 gSearchDir = 1;
  242.                 PerformTextSearch(dWin);
  243.                 break;
  244.             case SM_GotoAddress:
  245.                 GotoAddress(dWin);
  246.                 break;
  247.             }
  248.         }
  249.         break;
  250.     case OptionsMENU:
  251.         switch (menuItem) {
  252.         case OM_HiAscii:
  253.             gPrefs.asciiMode = !gPrefs.asciiMode;
  254.             if (gPrefs.asciiMode)
  255.                 gHighChar = 255;
  256.             else
  257.                 gHighChar = '~';
  258.             UpdateEditWindows();
  259.             break;
  260.         case OM_DecimalAddr:
  261.             gPrefs.decimalAddr = !gPrefs.decimalAddr;
  262.             UpdateEditWindows();
  263.             break;
  264.         case OM_Backups:
  265.             gPrefs.backupFlag = !gPrefs.backupFlag;
  266.             break;
  267.         case OM_Overwrite:
  268.             gOverwrite = !gOverwrite;
  269.             break;
  270.  
  271.         //LR :new options for working with files
  272.  
  273.         case OM_Compare:        //LR :file compares
  274.             DoCompare();
  275.             break;
  276.         case OM_ComparePref:    //LR :compare options
  277.             DoComparePref();
  278.             break;
  279.  
  280.         case OM_OtherFork:    //LR :I want to see both!
  281.             dWin = (EditWindowPtr) FrontWindow();
  282.             if( ((WindowPeek) dWin)->refCon == MyWindowID )
  283.             {
  284.                 short fork;
  285.                 EditWindowPtr ewin;
  286.  
  287.                 if( dWin->fork == FT_Data )
  288.                     fork = FT_Resource;
  289.                 else
  290.                     fork = FT_Data;    //LR %%9 :potential bug if more than 2 forks…
  291.  
  292.                 if( !!(ewin = LocateEditWindow( &dWin->fsSpec, fork )) )
  293.                 {
  294.                     SelectWindow( (WindowPtr)ewin );    // just select existing window
  295.                 }
  296.                 else    // try to open other fork in new window!
  297.                 {
  298.                     gForkMode = fork;
  299.                     OpenEditWindow( &dWin->fsSpec );
  300.                 }
  301.             }
  302.             break;
  303.         }
  304.         break;
  305.     }
  306.     HiliteMenu(0);
  307.     MyAdjustMenus();
  308. }
  309.  
  310.